﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections.Specialized;
using System.Xml.Serialization;
using System.Xml;

using YourDataDiscovery.UPSAddressValidation.Message;
using YourDataDiscovery.UPSAddressValidation.Common;
using System.IO;
using System.Net;

namespace UPSAddressValidation_Tester
{
    class Program
    {
        public static NameValueCollection appSettings = ConfigurationSettings.AppSettings;
        public static string upsAccessLicenseNumber;
        public static string upsUserId;
        public static string upsPassword;
        public static string upsAddressValidationLink;
        public static string responseFileOutputLocation;

        static void Main(string[] args)
        {
            /*Initialize Variables*/
            upsAccessLicenseNumber = appSettings["UPSAccessLicenseNumber"];
            upsUserId = appSettings["UPSUserId"];
            upsPassword = appSettings["UPSPassword"];
            upsAddressValidationLink = appSettings["UPSAddressValidationProdLink"];
            responseFileOutputLocation = appSettings["ResponseFileOutputLocation"];

            /*Construct UPS AV Request Message*/
            AddressValidationRequest avRequest = new AddressValidationRequest();
            RequestType reqType = new RequestType();
            TransactionReferenceType transRefType = new TransactionReferenceType();
            AddressType addrType = new AddressType();

            transRefType.CustomerContext = "YourDataDiscovery";
            transRefType.ToolVersion = "1.0001";
            reqType.TransactionReference = transRefType;
            reqType.RequestAction = "AV";
            avRequest.Request = reqType;
            avRequest.Address = addrType;

            /*Address Validation Request Sender*/
            AVRequestSender avReqSender = new AVRequestSender(upsAddressValidationLink, upsUserId, upsPassword, upsAccessLicenseNumber, avRequest);

            /*Retrieving and Saving Response File for Each Zip Code Starting from 00000 to 99999*/
            for (int i = 91731; i <= 91731; i++)
            {
               String zipCode = "00000" + i.ToString();
               zipCode = zipCode.Substring(zipCode.Length - 5);

               addrType.PostalCode = zipCode;
               avReqSender.SaveAddressValidationResponseToFile(responseFileOutputLocation, addrType.PostalCode + @".xml");
               Console.WriteLine("Address Validation for Postal Code: {0}", zipCode);
            }

            Console.ReadKey();
        }
    }
}
